package org.jetnuke.modules.calendar;

import org.apache.log4j.Logger;

import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.CalendarEventDAO;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.modules.calendar.utils.CalendarUtils;

public class WeekCalendar implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(WeekCalendar.class);

	int year = 0;
	int week = 0;

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		// Locale locale=(Locale)vc.get("_locale");
		GregorianCalendar today = new GregorianCalendar();
		if (year == 0) {
			year = today.get(Calendar.YEAR);
		}
		if (week == 0) {
			week = today.get(Calendar.WEEK_OF_YEAR);
		}

		CalendarUtils.putCommonObjects(vc);
		GregorianCalendar firstDayWeek = CalendarUtils.getFirstDayOfWeek(year,
				week);
		GregorianCalendar lastDayWeek = CalendarUtils.getLastDayOfWeek(year,
				week);
		vc.put("firstDayWeek", firstDayWeek);
		vc.put("lastDayWeek", lastDayWeek);
		vc.put("week", new Integer(week));
		vc.put("year", new Integer(year));
		List week_days = CalendarUtils.getDaysOfWeek(year, week);
		vc.put("week_days", week_days);
		HashMap events = new HashMap();
		Iterator it_day = week_days.iterator();
		CalendarEventDAO cedao = DataAccessDriver.getInst()
				.newCalendarEventDAO();
		// List
		// recurringEvents=cedao.selectRecurringEvents(ui.getID(),firstDayWeek);
		List recurringEvents = cedao.selectPeriodRecurringEvents(ui.getID(),
				firstDayWeek, lastDayWeek);
		while (it_day.hasNext()) {
			GregorianCalendar day_ = (GregorianCalendar) it_day.next();
			List dayEvents = cedao.selectDayEvents(ui.getID(), day_);
			List activeRecurringEventDay = CalendarUtils
					.getActiveRecurringEvent(recurringEvents, day_);
			dayEvents.addAll(activeRecurringEventDay);
			Collections.sort(dayEvents);
			events.put(day_, dayEvents);
		}
		vc.put("events", events);
		int previousWeek = week - 1;
		int previousYear = year;
		int nextYear = year;
		int nextWeek = week + 1;
		if (previousWeek < 1) {
			previousWeek = 53;
			previousYear = previousYear - 1;
		}
		if (nextWeek > 52) {
			nextWeek = 1;
			nextYear = nextYear + 1;
		}
		vc.put("nextWeek", new Integer(nextWeek));
		vc.put("nextYear", new Integer(nextYear));
		vc.put("previousWeek", new Integer(previousWeek));
		vc.put("previousYear", new Integer(previousYear));

		ActionForward returnActionForward = new ActionForward(op, info);
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return returnActionForward;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public void setWeek(int week) {
		this.week = week;
	}

}
